/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_goods;


import com.yuecai.modules.security.JwtUserDto;
import com.yuecai.modules.yc_category.vo.YcGoodsDetails;
import com.yuecai.modules.yc_goods.service.YcGoodsService;
import com.yuecai.modules.yc_goods.service.dto.YcGoodsQueryCriteria;
import com.yuecai.modules.yc_goods_sku.domain.YcGoodsSku;
import com.yuecai.modules.yc_goods_sku.service.dto.YcGoodsSkuDto;
import com.yuecai.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @website https://el-admin.vip
* @author dm
* @date 2020-06-24
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "yc_goods管理")
@RequestMapping("/good/ycGoods")
public class YcGoodsController {

    private final YcGoodsService ycGoodsService;



    @GetMapping("/details")
    public ResponseEntity<Object> details(Long goodId) throws Exception {
        JwtUserDto currentUser = (JwtUserDto) SecurityUtils.getCurrentUser();
        List<YcGoodsSkuDto> details = ycGoodsService.details(goodId);
        //List<YcGoodsSku> details = ycGoodsService.details(goodId);
        if (currentUser==null||!currentUser.getUser().isRealName()){
            details.forEach(item->{
                item.setOriginalPrice(null);
                item.setSellPrice(null);
                item.setUnitPrice(null);
            });
        }
        return new ResponseEntity<>(details,HttpStatus.OK);
    }


    @GetMapping
    @ApiOperation("查询yc_goods")
    public ResponseEntity<Object> query(YcGoodsQueryCriteria criteria, Pageable pageable){
        criteria.setStatus(true);
        return new ResponseEntity<>(ycGoodsService.findYcGoodsVOs(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/id")
    @ApiOperation("查询yc_goods")
    public ResponseEntity<Object> findById(Long id) throws Exception {

        try {
            YcGoodsDetails byIdYcGoodsVo = ycGoodsService.findByIdYcGoodsVo(id);
            JwtUserDto currentUser = (JwtUserDto) SecurityUtils.getCurrentUser();
            if (currentUser==null||!currentUser.getUser().isRealName()){
                byIdYcGoodsVo.setOriginalPrice(null);
                byIdYcGoodsVo.setVipDiscount(null);
            }
            return new ResponseEntity<>(byIdYcGoodsVo,HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

    }


}